<?php
/**
 *   OPENSHOP 管理中心聚集页设定程序
 * 
 *   @package     OpenShop
 *   @version     $Id: createdynamic.php,v 1.0 2009/07/29 11:18:17 modified $
 *   @author      杨敬君 <aqkt_436@hotmail.com>
 */
define('IN_OS', true);
require(dirname(__FILE__) . '/includes/init.php');
include_once(ROOT_PATH."includes/fckeditor/fckeditor.php");
include_once("Cdycommand.php");
//读取项目库ID
$lib=$_REQUEST['lib'];
$ty=$_REQUEST['ty'];
$sort=$_REQUEST['sort'];
$act=$_REQUEST['act'];
$textnum=$_REQUEST['num'];
$annexFolder = "../Simage";//附件存放点，默认为：annex
$smallFolder = "smallimg";//缩略图存放路径，注：必须是放在 $annexFolder下的子目录，默认为：smallimg
$markFolder = "mark";//水印图片存放处
$includeFolder = "includes";
require("".$includeFolder."/upfile.php");          
//读取XML配置
    $doc = new DOMDocument();
    $config_file = ROOT_PATH . 'themes/' . $GLOBALS['_CFG']['template'].'/library/config.xml';
    $doc->load($config_file);
    $libs = $doc->getElementsByTagName( "lib" );
	foreach( $libs as $lib_node){	
		if($lib==$lib_node->getAttribute("id")){
		     $slibs=$lib_node->getElementsByTagName("data");
	  	      foreach ($slibs as $slib_node =>$key){	  	  	
	  	  	   if($ty==$key->getAttribute("datatype")){  
                  if($ty<>"5"){//非图文
	  	  	   	  //  $textnum=$key->getAttribute("num");//数量	  	  	   	   	    
	  	  		    $height=$key->getAttribute("height");//高度
	  	  		    $width=$key->getAttribute("width");//宽度
	  	  		    $islink=$key->getAttribute("islink");//是否链接1是0否
	  	  		    $Sort=$key->getAttribute("Sort");//排序
	  	  		    $length=$key->getAttribute("length");//文本框串长度
	  	  		    if($ty=="1" or $ty=="2"){
	  	  		    	 $target=$key->getAttribute("target");//除图片/flash/html/图文 外的链接目标
	  	  		    }	  	  		    
                  }else{ //图文
                  	 //  $imgnum=$key->getAttribute("num");//图文类型多少条
	  	  		       $imglibs=$lib_node->getElementsByTagName("img");//读取图文(图片)	  	  		       
	  	  		       foreach ($imglibs as $aimglibs =>$imgkey){
	  	  		       	   $imgheight=$imgkey->getAttribute("height");	//图片高度  
	  	  		       	   $imgwidth=$imgkey->getAttribute("width");    //图片宽度
	  	  		       	   $imgislink=$imgkey->getAttribute("islink");  //图片是否链接1是0否
	  	  		       	   $imgtarget=$imgkey->getAttribute("target");  //图片链接目标
	  	  		       }
	  	  		       $textlibs=$lib_node->getElementsByTagName("text");//读取图文(文字)
	  	  		       foreach ($textlibs as $atextlibs =>$textkey){ 
	  	  		       	    $atextnum=$textkey->getAttribute("num");//读取文字多少条数
	  	  		       	    $atexttarget=$textkey->getAttribute("target");//文字链接目标	       	  
	  	  		       }
	  	  		    }
	  	  	   }
	  	   }
		}
	}
$typevalue=get_dynamicDB($sort,$lib,$ty,$textnum,$atextnum);//类型的值

//执行sql操作
switch ($act){
	case "add":			
if($ty=="1"){//文字
		
		switch (is_dyOnly($lib,$ty,$sort)){
			case 1://更新数据
		    $sql="update ".$os->table('dynamic')." set ";
		    for($i=1;$i<=$textnum;$i++){
		          $sql=$sql."dy_text".$i."='".$_REQUEST["dy_Text$i"]."',";
		       if($i<>$textnum){
		          $sql=$sql."dy_TextCUrl".$i."='".$_REQUEST["dy_TextCUrl$i"]."',";
		       }else {
		   	      $sql=$sql."dy_TextCUrl".$i."='".$_REQUEST["dy_TextCUrl$i"]."'";
		       }
		    }
		          $sql=$sql." where dy_Project='$lib' and dy_sort='$sort'";		
		          break;
		    default://添加数据
		    $sql="insert into ".$os->table('dynamic')."(dy_Project,dy_type,dy_sort,";		
		    for($i=1;$i<=$textnum;$i++){		   
		    $sql=$sql."dy_text".$i.",";
		 	  if($i<>$textnum){		      		 
		        $sql=$sql."dy_TextCUrl".$i.",";
			  }else{				 	 
		        $sql=$sql."dy_TextCUrl".$i."";			
			  }		
		    };		
		    $sql=$sql.") values ('$lib','$ty','$sort',";
		    for($i=1;$i<=$textnum;$i++){
               $sql=$sql."'".$_REQUEST["dy_Text$i"]."',";	
			if($i<>$textnum){		      		 
		      $sql=$sql."'".$_REQUEST["dy_TextCUrl$i"]."',";
			}else{				 	 
		      $sql=$sql."'".$_REQUEST["dy_TextCUrl$i"]."'";				
			}		 
		    };		
		    $sql=$sql.")";
		    break;		
		}
		$db->query($sql);		
		$link[0]['text'] = '返回上一级';
	    $link[0]['href'] = "addtest.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
		sys_msg('设置成功！', 0, $link);
		break;
}elseif ($ty=="2"){//图片	  
			//echo $textnum;
			for($i=1;$i<=$textnum;$i++){
				//验证配置文件是否需要链接 如果需要 则验证url是否为空
				if($islink==0){
					if($_POST["dy_PCUrl$i"]==""){
					$link[0]['text'] = '返回上一级';
	                $link[0]['href'] = "addtest.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
	                $er="url".$i."不能为空";
	                sys_msg($er, 0, $link);
				    }				
				}	
			 }	
             switch (is_dyOnly($lib,$ty,$sort)){
			 		case 1:	//更新数据	 			
			 			//echo $textnum;
			 			$img = new UPImages($annexFolder,$smallFolder,$includeFolder,$lib,$ty,$sort,$textnum); 
			            $sql="update ".$os->table('dynamic')." set ";
		                for($i=1;$i<=$textnum;$i++){
		                    if(@empty($_FILES["dy_PPath$i"]["name"])==FALSE){		         	         
		                		$sql=$sql."dy_PPath".$i."='$annexFolder/".$img->upLoad("dy_PPath$i",$i)."',";		                	
		                	}	             
		                if($i<>$textnum){
		                  $sql=$sql."dy_PCUrl".$i."='".$_POST["dy_PCUrl$i"]."',";
		                }else {
		   	              $sql=$sql."dy_PCUrl".$i."='".$_POST["dy_PCUrl$i"]."'";
		                }
		                }
		                 $sql=$sql." where dy_Project='$lib' and dy_sort='$sort'";		                 
			 		break;
			 		default:	//添加数据	
			 			$img = new UPImages($annexFolder,$smallFolder,$includeFolder,$lib,$ty,$sort,$textnum); 	 			
			            $sql="insert into ".$os->table('dynamic')."(dy_Project,dy_type,dy_sort,";		
		                for($i=1;$i<=$textnum;$i++){
		                	if(@empty($_FILES["dy_PPath$i"]["name"])==FALSE){
		                		$sql=$sql."dy_PPath".$i.","; 
		                	}
		                	if($i<>$textnum){
		                		$sql=$sql."dy_PCUrl".$i.",";
		                	}else{
			            	$sql=$sql."dy_PCUrl".$i."";
		                	}		
		                };		
		                $sql=$sql.") values ('$lib','$ty','$sort',";
		                for($i=1;$i<=$textnum;$i++){
		                	if(@empty($_FILES["dy_PPath$i"]["name"])==FALSE){
		                	$sql=$sql."'$annexFolder/".$img->upLoad("dy_PPath$i",$i)."',";	
		                	}
			            if($i<>$textnum){		      		 
		                  $sql=$sql."'".$_POST["dy_PCUrl$i"]."',";
			            }else{				 	 
		                  $sql=$sql."'".$_POST["dy_PCUrl$i"]."'";				
			            }		 
		                };		
		                $sql=$sql.")";
			 			break;			 		
			 	}
			 	//echo $sql;
			 	$db->query($sql);		
		        $link[0]['text'] = '返回上一级';
	            $link[0]['href'] = "addtest.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
		        sys_msg('设置成功！', 0, $link);
}elseif ($ty=="3"){		//flash	
			 switch (is_dyOnly($lib,$ty,$sort)){
			 		case 1:	//更新数据
			 		if(@empty($_FILES["flaname"]["name"])==FALSE){
			 		$img = new UPImages($annexFolder,$smallFolder,$includeFolder,$lib,$ty,$sort,$textnum);
			        $sql="update ".$os->table('dynamic')." set dy_FPath='$annexFolder/".$img->upLoad("flaname","1")."' where dy_Project='$lib' and dy_sort='$sort'";	             
                    $db->query($sql);	
			 		}else {
			 		$link[0]['text'] = '返回上一级';
	                $link[0]['href'] = "addtest.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
		            sys_msg('请上传文件！', 0, $link);	
			 		}
			        break;
			 		default://添加数据			 			
			 		if(@empty($_FILES["flaname"]["name"])==FALSE){
			 	    $img = new UPImages($annexFolder,$smallFolder,$includeFolder,$lib,$ty,$sort,$textnum); 	 			
			        $sql="insert into ".$os->table('dynamic')."(dy_Project,dy_type,dy_sort,dy_FPath) values('$lib','$ty','$sort','$annexFolder/".$img->upLoad("flaname","1")."')";
			 		$db->query($sql);	
			 		}else{
			 	    $link[0]['text'] = '返回上一级';
	                $link[0]['href'] = "addtest.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
		            sys_msg('请上传文件！', 0, $link);			 			
			 		}
		            break;			 		
			 	}			 		
		        $link[0]['text'] = '返回上一级';
	            $link[0]['href'] = "addtest.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
		        sys_msg('设置成功！', 0, $link);
}elseif ($ty=="4"){//html
			 switch (is_dyOnly($lib,$ty,$sort)){
			 		case 1:	//更新数据	 						 	
			        $sql="update ".$os->table('dynamic')." set dy_Html='".$_REQUEST["HTMLN"]."' where dy_Project='$lib' and dy_sort='$sort'";		             
			 		break;
			 		default:	//添加数据				 		
			        $sql="insert into ".$os->table('dynamic')."(dy_Project,dy_type,dy_sort,dy_Html) values('$lib','$ty','$sort','".$_REQUEST["HTMLN"]."')";	
		            break;			 		
			 	}
			 	$db->query($sql);		
		        $link[0]['text'] = '返回上一级';
	            $link[0]['href'] = "addtest.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
		        sys_msg('设置成功！', 0, $link);
		}else{//图文
		$img = new UPImages($annexFolder,$smallFolder,$includeFolder,$lib,$ty,$sort,$textnum);
		switch (is_dyOnly($lib,$ty,$sort)){
			case 1://更新数据
		    $sql="update ".$os->table('dynamic')." set ";
			if(@empty($_FILES["dy_PPath1"]["name"])==FALSE){
			$sql=$sql." dy_PPath1='$annexFolder/".$img->upLoad("dy_PPath1","1")."'," ;
			}
			$sql=$sql."dy_PCUrl1='".$_POST["dy_PCUrl1"]."',";
		    for($i=1;$i<=$atextnum;$i++){
		          $sql=$sql."dy_text".$i."='".$_REQUEST["dy_Text$i"]."',";
		       if($i<>$atextnum){
		          $sql=$sql."dy_TextCUrl".$i."='".$_REQUEST["dy_TextCUrl$i"]."',";
		       }else {
		   	      $sql=$sql."dy_TextCUrl".$i."='".$_REQUEST["dy_TextCUrl$i"]."'";
		       }
		    }
		          $sql=$sql." where dy_Project='$lib' and dy_sort='$sort'";		
		          break;
		    default://添加数据
		    $sql="insert into ".$os->table('dynamic')."(dy_Project,dy_type,dy_sort,dy_PPath1,dy_PCUrl1,";		
		    for($i=1;$i<=$atextnum;$i++){		   
		    $sql=$sql."dy_text".$i.",";
		 	  if($i<>$atextnum){		      		 
		        $sql=$sql."dy_TextCUrl".$i.",";
			  }else{				 	 
		        $sql=$sql."dy_TextCUrl".$i."";			
			  }		
		    };		
		    $sql=$sql.") values ('$lib','$ty','$sort','$annexFolder/".$img->upLoad("dy_PPath1","1")."','".$_POST["dy_PCUrl1"]."',";
		    for($i=1;$i<=$atextnum;$i++){
               $sql=$sql."'".$_REQUEST["dy_Text$i"]."',";	
			if($i<>$atextnum){		      		 
		      $sql=$sql."'".$_REQUEST["dy_TextCUrl$i"]."',";
			}else{				 	 
		      $sql=$sql."'".$_REQUEST["dy_TextCUrl$i"]."'";				
			}		 
		    };		
		    $sql=$sql.")";
		    break;		
		}
			$db->query($sql);		
		    $link[0]['text'] = '返回上一级';
	        $link[0]['href'] = "addtest.php?lib=$lib&ty=$ty&sort=$sort&num=$textnum";
		    sys_msg('设置成功！', 0, $link);	
		}	
	default:		
		break;	
}
/*
 * 文字呈现
 */
function text($i,$value){	
		for($a=1;$a<=$i;$a++){
	    $result=$result."<tr>
	      <td width='8%' class='first-cell'>标题：</td>
	      <td><input type='text' name='dy_Text$a' value=".$value["Text$a"]."></td>
	      <td width='8%'>链接：</td>
	      <td colspan='3'><input type='text' name='dy_TextCUrl$a' value=".$value["Url$a"]."></td>
	      </tr>
	      ";
		}	
		return $result;		
}
/**
 * 图片呈现
 */
function image($i,$h,$w,$value){
		for($a=1;$a<=$i;$a++){
	    $result=$result."<tr>
        <td class='first-cell'>图片".$a."：</td>
        <td><img src=".$value["dy_PPath$a"]." width='$w' height='$h'>        
        <input type='file' name='dy_PPath$a' size=45> </td>
        <td>高度：</td>
        <td width='7%'>$h.px</td>
        <td width='9%'>宽度：</td>
        <td>$w.px</td>
        </tr><tr>
          <td class='first-cell'>Url".$a.":</td>
          <td colspan=5><input type=text name='dy_PCUrl$a' size=57 value=".$value["dy_PCUrl$a"]."></td>
        </tr>";
		}	
		return $result;
}
/**
 * flash呈现
 */
function flash($i,$h,$w,$value){
		for($a=1;$a<=$i;$a++){
	    $result=$result."<tr>
        <td class='first-cell'>flash：</td>
        <td colspan='7'>
        <object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0 width=$w height=$h>
          <param name=movie value=$value[dy_FPath]>
          <param name=quality value=high>
          <embed src=$value[dy_FPath] quality=high pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash width=$w height=$h></embed>
        </object>
        <input type='file' name='flaname'>
        高度：$h.px&nbsp;宽度:$w.px
        </td>
        </tr>";
		}	
		return $result;
}
/**
 * 图文呈现
 * 
 */
function imtext($h,$w,$atextnum,$value){	
	    $result="<tr>
                 <td class='first-cell'>图片</td> 
                 <td>
                 <img src='$value[dy_PPath1]' width='$w' height='$h'>
                 <input type='file' name='dy_PPath1' size=30></td>
                 <td>高度</td>
                 <td>$h.px</td>
                 <td>宽度</td>
                 <td colspan='3'>$w.px</td>
                 </tr>
                 <tr>
                 <td class='first-cell'>Url</td>
                 <td colspan=7><input type=text name=dy_PCUrl1 size=46 value=$value[dy_PCUrl1]></td></tr>";
         for($b=1;$b<=$atextnum;$b++){
           $result=$result."<tr>
                            <td class='first-cell'>文字</td>
                            <td><input type='text' name='dy_Text$b' maxlength='8' value=".$value["Text$b"]."></td>
                            <td colspan='3'>文字链接</td>
                            <td colspan='3'><input type='text' name='dy_TextCUrl$b' value=".$value["Url$b"]."></td>
                            </tr>";
          }	
		return $result;
}
/**
 * 判断项目库是否存在
 * @param   string      $lib 项目库名
 * @param   string      $ty 类型
 * @param   string      $sort 排序
 * @return  boolean
 */
function is_dyOnly($lib,$ty,$sort)
{
    $res = $GLOBALS['db']->getOne("SELECT COUNT(*) FROM " . $GLOBALS['os']->table('dynamic') .
                                  " WHERE dy_Project = '$lib' and dy_Type='$ty' and dy_Sort='$sort'");
    return $res;
}    
 /* 编辑器 */
$editor = new FCKeditor('HTMLN');
$editor->BasePath   = "../includes/fckeditor/";
$editor->ToolbarSet = "Normal";
$editor->Width      = "95%";
$editor->Height     = "300";
$editor->Value      = $typevalue[dy_Html];
$fckeditor = $editor->CreateHtml();
$smarty->assign('text',text($textnum,$typevalue));
$smarty->assign('image',image($textnum,$height,$width,$typevalue));
$smarty->assign('flash',flash($textnum,$height,$width,$typevalue));
$smarty->assign('imtext',imtext($imgheight,$imgwidth,$atextnum,$typevalue));
$smarty->assign('fckeditor', $fckeditor);
$smarty->assign('ur_here','填充内容');
$guide['level_two']="addtest";
$smarty->assign('textnum',$textnum);
$smarty->assign('guide',$guide);
$smarty->assign('type',$ty);
$smarty->assign('lib',$lib);
$smarty->assign('sort',$sort);
$smarty->display("index.tpl");
?>